package pl.com.qapps.datastore;

import com.google.appengine.api.datastore.Transaction;
import com.google.apphosting.api.DatastorePb.CompiledCursor;
import com.google.apphosting.api.DatastorePb.CompiledCursor.Position;

public class ApiUtils {

	private ApiUtils() {
	}

	/**
	 * If {@code txn} is not null and not active, throw
	 * {@link IllegalStateException}.
	 */
	public static void ensureTxnActive(Transaction txn) {
		if (txn != null && !txn.isActive()) {
			throw new IllegalStateException(
					"Transaction with which this operation is "
							+ "associated is not active.");
		}
	}
	
	public CompiledCursor newFakeCompiledCursor(int pos) {
		CompiledCursor cursor = new CompiledCursor();
		Position position = new Position();
		position.setStartKey(String.valueOf(pos));
		cursor.addPosition(position);
		return cursor;
	}
}
